package com.ww.editsms;

import java.io.File;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.Html;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.chinawangw.R;
import com.ww.editsms.util.DeviceUtil;
import com.ww.editsms.util.MsgDialog;
import com.ww.editsms.util.StringUtils;

/**
 * @version 2012-9-12
 * @author wangw
 */
public class MyActivity extends Activity {
	private ProgressDialog mProgressDialog;//发送请求 等待的dialog
	public int type = -1;
	public static final String ANIM = "anim";
	public static String theme;
	public ImageButton right;
	public PopupWindow popupWindow;
	AlertDialog dlg = null;
	private ProgressBar refresh;
	ImageView right_src;

	/**
	 * 初始化皮肤.
	 * @param title 标题栏文字
	 */
	public void setTitle(String title) {
		try {
			TextView textView = (TextView) findViewById(R.id.title);
			textView.setText(title);
		} catch (Exception e) {
		}

		try {
			Button back = (Button) findViewById(R.id.back);
			back.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					finish();
				}
			});
		} catch (Exception e) {
		}
	}

	/**
	 * 初始化皮肤.
	 * @param title 标题栏文字
	 */
	public void setTitle(int title) {
		setTitle(getReString(title));
	}

	/**
	 * 设置右上角按钮图标.
	 * @param image 资源
	 */
	public void setRightButton(int image) {
		try {
			right = (ImageButton) findViewById(R.id.right);
			right.setImageResource(image);
			right.setVisibility(View.VISIBLE);
		} catch (Exception e) {
		}
	}

	/**
	 * 设置右上角按钮图标.
	 * @param image 资源
	 */
	public void setRightText(String text) {
		try {
			TextView right_txt = (TextView) findViewById(R.id.right_txt);
			right_txt.setText(text);
			right_txt.setVisibility(View.VISIBLE);
			right = (ImageButton) findViewById(R.id.right);
			right.setVisibility(View.VISIBLE);
		} catch (Exception e) {
		}
	}

	/**
	 * 设置右上角按钮图标.
	 * @param image 资源
	 */
	public void setRightText(int text) {
		try {
			TextView right_txt = (TextView) findViewById(R.id.right_txt);
			right_txt.setText(text);
			right_txt.setVisibility(View.VISIBLE);
			right = (ImageButton) findViewById(R.id.right);
			right.setVisibility(View.VISIBLE);
		} catch (Exception e) {
		}
	}


	/**
	 * 公共手机返回按钮事件.
	 * @param keyCode keyCode
	 * @param event  KeyEvent
	 * @return  boolean
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			back();
			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}

	public void back() {
		finish();
	}

	/**
	 * 调用系统的分享功能.
	 * @param subject 分享的主题
	 * @param content  分享的内容
	 * @param image  分享的图片(完整的本地路径)
	 */
	public void share(String subject, String content, String path) {
		if(!DeviceUtil.checkNet(this)){
		}else if(!StringUtils.isNotEmpty(content)){
			MsgDialog.show(this, "空内容无法分享");
		}else{
			Intent intent = new Intent(Intent.ACTION_SEND); // 启动分享发送到属性
			if (path == null) {
				intent.setType("text/plain"); // 分享发送到数据类型
			} else {
				if (path != null) {
					File f = new File(path);
					if (f.exists()) {
						intent.setType("image/png");
						Uri u = Uri.fromFile(f);
						intent.putExtra(Intent.EXTRA_STREAM, u);
					} else {
						intent.setType("text/plain"); // 分享发送到数据类型
					}
				} 
			}
			intent.putExtra(Intent.EXTRA_SUBJECT, subject); // 分享的主题
			intent.putExtra(Intent.EXTRA_TEXT, content.replaceAll("<.*?>", "")); // 分享的内容
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // 允许intent启动新的activity
			startActivity(Intent.createChooser(intent, "分享")); //目标应用选择对话框的标题
		}
	}

	/**
	 * 显示提示信息.
	 * @param message 提示信息
	 */
	public void showMessage(String message) {
		MsgDialog.show(this, message);
	}

	/**
	 * 获取string
	 * @param string R.string.x
	 * @return  ""
	 */
	public String getReString(int string) {
		return getResources().getString(string);
	}

	/**
	 * 显示提示信息.
	 * @param message 提示信息
	 */
	public void showMessage(int message) {
		MsgDialog.show(this, message);
	}

	/**
	 * 显示提示信息:没有可用网络.
	 */
	public void showServerError() {
		showMessage(getResources().getString(R.string.server_error));
	}

	/**
	 * 显示提示信息.
	 * @param message 提示信息
	 */
	public void showMessageOfTop(String message) {
		LayoutInflater inflater = LayoutInflater.from(MyActivity.this);
		View view = inflater.inflate(R.layout.core_toast, (ViewGroup) findViewById(R.id.toast_layout_root));
		TextView textView = (TextView) view.findViewById(R.id.text);
		textView.setText(Html.fromHtml(message));
		Toast toast = new Toast(MyActivity.this);
		toast.setDuration(Toast.LENGTH_LONG);
		toast.setView(view);
		toast.setGravity(Gravity.TOP, 0, 96);
		toast.show();
	}


	public void failView(String txt, int img) {
		ImageView imgerror = (ImageView) findViewById(R.id.imgerror);
		TextView neterror = (TextView) findViewById(R.id.neterror);
		LinearLayout nonetwork = (LinearLayout) findViewById(R.id.nonetwork);
		nonetwork.setVisibility(View.VISIBLE);
		if (img != 0) {
			imgerror.setImageResource(img);
		}else{
			imgerror.setVisibility(View.GONE);
		}
		if (StringUtils.isNotEmpty(txt)) {
			neterror.setText(txt);
		}
	}
	
	public void netView() {
		LinearLayout nonetwork = (LinearLayout) findViewById(R.id.nonetwork);
		nonetwork.setVisibility(View.VISIBLE);
	}

	/**
	 * 显示提示信息.
	 * @param message 提示信息
	 */
	public void onAutoRefresh() {
		refresh.setVisibility(View.VISIBLE);
	}

	/**
	 * 显示提示信息.
	 * @param message 提示信息
	 */
	public void completeAutoRefresh() {
		refresh.setVisibility(View.GONE);
	}

	/**
	 * 创建进度框.
	 * @param mContext Context
	 */
	public void showProgressDialog(Context mContext, String tip) {
		if (mProgressDialog == null) {
			ProgressDialog dialog = new ProgressDialog(mContext);
			dialog.setIndeterminate(true);
			dialog.setCancelable(true);
			mProgressDialog = dialog;
		}
		try {
			mProgressDialog.show();
			LayoutInflater inflater = LayoutInflater.from(MyActivity.this);
			View view = inflater.inflate(R.layout.core_loading, null);
			TextView textView = (TextView) view.findViewById(R.id.oaprogresstitle);
			String temp = mContext.getResources().getString(R.string.loading);
			if (StringUtils.isNotEmpty(tip)) {
				temp = tip;
			}
			textView.setText(temp);
			mProgressDialog.setContentView(view);
		} catch (Exception e) {
		}
	}

	/**
	 * 创建进度框.
	 * @param mContext Context
	 */
	public void showProgressDialog(Context mContext) {
		if (mProgressDialog == null) {
			ProgressDialog dialog = new ProgressDialog(mContext);
			dialog.setIndeterminate(true);
			dialog.setCancelable(true);
			mProgressDialog = dialog;
		}
		try {
			mProgressDialog.show();
			mProgressDialog.setContentView(R.layout.core_loading);
		} catch (Exception e) {
		}
	}

	public void hideProgressDialog() {
		if (mProgressDialog != null) {
			mProgressDialog.dismiss();
		}
	}

	
}
